Exponential Backoff for Google APIs
工作流概述
这是一个包含8个节点的中等工作流,主要用于自动化处理各种任务。
工作流源代码
{
"id": "2NhqmUqW3KruEkaE",
"meta": {
"instanceId": "d868e3d040e7bda892c81b17cf446053ea25d2556fcef89cbe19dd61a3e876e9"
},
"name": "Exponential Backoff for Google APIs",
"tags": [
{
"id": "nezaWFCGa7eZsVKu",
"name": "Utility",
"createdAt": "2024-11-13T18:08:08.207Z",
"updatedAt": "2024-11-13T18:08:08.207Z"
}
],
"nodes": [
{
"id": "5d6b1730-33c5-401c-b73f-2b7ea8eedfe3",
"name": "When clicking ‘Test workflow’",
"type": "n8n-nodes-base.manualTrigger",
"position": [
-580,
-80
],
"parameters": {},
"typeVersion": 1
},
{
"id": "6726b630-597c-46cf-8839-75cd80108f2f",
"name": "Exponential Backoff",
"type": "n8n-nodes-base.code",
"position": [
160,
120
],
"parameters": {
"mode": "runOnceForEachItem",
"jsCode": "// Define the retry count (coming from a previous node or set manually)
const retryCount = $json[\"retryCount\"] || 0; // If not present, default to 0
const maxRetries = 5; // Define the maximum number of retries
const initialDelay = 1; // Initial delay in seconds (1 second)
// If the retry count is less than the max retries, calculate the delay
if (retryCount < maxRetries) {
const currentDelayInSeconds = initialDelay * Math.pow(2, retryCount); // Exponential backoff delay in seconds
// Log the delay time for debugging
console.log(`Waiting for ${currentDelayInSeconds} seconds before retry...`);
return {
json: {
retryCount: retryCount + 1, // Increment retry count
waitTimeInSeconds: currentDelayInSeconds, // Pass the delay time in seconds
status: 'retrying',
}
};
} else {
// If max retries are exceeded, return a failure response
return {
json: {
error: 'Max retries exceeded',
retryCount: retryCount,
status: 'failed'
}
};
}
"
},
"typeVersion": 2
},
{
"id": "605b8ff0-aa19-42dd-8dbb-aa12380ac4bc",
"name": "Stop and Error",
"type": "n8n-nodes-base.stopAndError",
"position": [
760,
120
],
"parameters": {
"errorMessage": "Google Sheets API Limit has been triggered and the workflow has stopped"
},
"typeVersion": 1
},
{
"id": "97818e8b-e0cc-4a49-8797-43e02535740f",
"name": "Loop Over Items",
"type": "n8n-nodes-base.splitInBatches",
"position": [
-360,
-80
],
"parameters": {
"options": {}
},
"typeVersion": 3
},
{
"id": "0583eabd-bd97-4330-8a38-b2aed3a90c37",
"name": "Google Sheets",
"type": "n8n-nodes-base.googleSheets",
"onError": "continueErrorOutput",
"position": [
-120,
20
],
"parameters": {
"options": {},
"sheetName": {
"__rl": true,
"mode": "name",
"value": "Sheet1"
},
"documentId": {
"__rl": true,
"mode": "url",
"value": "https://docs.google.com/spreadsheets/d/1_gxZl6n_AYPHRFRTWfhy7TZnhEYuWzh8UvGdtWCD3sU/edit?gid=0#gid=0"
},
"authentication": "serviceAccount"
},
"credentials": {
"googleApi": {
"id": "lm7dPHYumCy6sP6k",
"name": "AlexK1919 Google Service"
}
},
"typeVersion": 4.5
},
{
"id": "0d8023f8-f7ac-4303-b18e-821690cc9f94",
"name": "Wait",
"type": "n8n-nodes-base.wait",
"position": [
360,
120
],
"webhookId": "f1651aa1-6497-4496-9e07-240dcf1852f3",
"parameters": {
"amount": "={{ $json[\"waitTime\"] }}"
},
"typeVersion": 1.1
},
{
"id": "72e0001e-f99b-4d57-9006-4a4dd5d3d8d5",
"name": "Check Max Retries",
"type": "n8n-nodes-base.if",
"position": [
560,
120
],
"parameters": {
"options": {},
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "51e191cb-af20-423b-9303-8523caa4ae0d",
"operator": {
"type": "number",
"operation": "gt"
},
"leftValue": "={{ $('Exponential Backoff').item.json[\"retryCount\"] }}",
"rightValue": 10
}
]
}
},
"typeVersion": 2.2
},
{
"id": "2ea14bb0-4313-4595-811d-729ca6d37420",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"position": [
100,
-80
],
"parameters": {
"color": 3,
"width": 820,
"height": 460,
"content": "# Exponential Backoff for Google APIs
## Connect these nodes to any Google API node such as the Google Sheets node example in this workflow"
},
"typeVersion": 1
}
],
"active": false,
"pinData": {},
"settings": {
"executionOrder": "v1"
},
"versionId": "729e3a54-6238-4e4c-833e-8e37dba16dbb",
"connections": {
"Wait": {
"main": [
[
{
"node": "Check Max Retries",
"type": "main",
"index": 0
}
]
]
},
"Google Sheets": {
"main": [
[
{
"node": "Loop Over Items",
"type": "main",
"index": 0
}
],
[
{
"node": "Exponential Backoff",
"type": "main",
"index": 0
}
]
]
},
"Loop Over Items": {
"main": [
[],
[
{
"node": "Google Sheets",
"type": "main",
"index": 0
}
]
]
},
"Check Max Retries": {
"main": [
[
{
"node": "Stop and Error",
"type": "main",
"index": 0
}
],
[
{
"node": "Google Sheets",
"type": "main",
"index": 0
}
]
]
},
"Exponential Backoff": {
"main": [
[
{
"node": "Wait",
"type": "main",
"index": 0
},
{
"node": "Check Max Retries",
"type": "main",
"index": 0
}
]
]
},
"When clicking ‘Test workflow’": {
"main": [
[
{
"node": "Loop Over Items",
"type": "main",
"index": 0
}
]
]
}
}
}
功能特点
- 自动检测新邮件
- AI智能内容分析
- 自定义分类规则
- 批量处理能力
- 详细的处理日志
技术分析
节点类型及作用
- Manualtrigger
- Code
- Stopanderror
- Splitinbatches
- Googlesheets
复杂度评估
配置难度:
维护难度:
扩展性:
实施指南
前置条件
- 有效的Gmail账户
- n8n平台访问权限
- Google API凭证
- AI分类服务订阅
配置步骤
- 在n8n中导入工作流JSON文件
- 配置Gmail节点的认证信息
- 设置AI分类器的API密钥
- 自定义分类规则和标签映射
- 测试工作流执行
- 配置定时触发器(可选)
关键参数
| 参数名称 | 默认值 | 说明 |
|---|---|---|
| maxEmails | 50 | 单次处理的最大邮件数量 |
| confidenceThreshold | 0.8 | 分类置信度阈值 |
| autoLabel | true | 是否自动添加标签 |
最佳实践
优化建议
- 定期更新AI分类模型以提高准确性
- 根据邮件量调整处理批次大小
- 设置合理的分类置信度阈值
- 定期清理过期的分类规则
安全注意事项
- 妥善保管API密钥和认证信息
- 限制工作流的访问权限
- 定期审查处理日志
- 启用双因素认证保护Gmail账户
性能优化
- 使用增量处理减少重复工作
- 缓存频繁访问的数据
- 并行处理多个邮件分类任务
- 监控系统资源使用情况
故障排除
常见问题
邮件未被正确分类
检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。
Gmail认证失败
确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。
调试技巧
- 启用详细日志记录查看每个步骤的执行情况
- 使用测试邮件验证分类逻辑
- 检查网络连接和API服务状态
- 逐步执行工作流定位问题节点
错误处理
工作流包含以下错误处理机制:
- 网络超时自动重试(最多3次)
- API错误记录和告警
- 处理失败邮件的隔离机制
- 异常情况下的回滚操作